// 游戏进度追踪系统
class ProgressTracker {
    constructor() {
        this.stats = {
            chaptersCompleted: 0,
            branchesExplored: new Set(),
            endingsUnlocked: new Set(),
            totalPlaytime: 0,
            choicesMade: 0,
            achievementsUnlocked: new Set(),
            gameStartTime: Date.now(),
            sessionStartTime: Date.now()
        };
        
        this.init();
    }
    
    init() {
        this.loadProgressData();
        this.setupProgressTracking();
        this.startPlaytimeTracking();
        console.log('✓ 进度追踪系统已初始化');
    }
    
    setupProgressTracking() {
        // 监听游戏事件
        window.addEventListener('chapterCompleted', (e) => {
            this.onChapterCompleted(e.detail.chapter);
        });
        
        window.addEventListener('branchExplored', (e) => {
            this.onBranchExplored(e.detail.branchId);
        });
        
        window.addEventListener('endingReached', (e) => {
            this.onEndingReached(e.detail.endingId);
        });
        
        // 定期更新显示
        setInterval(() => {
            this.updateProgressDisplay();
        }, 1000);
    }
    
    startPlaytimeTracking() {
        setInterval(() => {
            if (this.isGameActive()) {
                this.stats.totalPlaytime += 1000;
                this.saveProgressData();
            }
        }, 1000);
    }
    
    isGameActive() {
        const gameScreen = document.getElementById('gameScreen');
        return gameScreen && gameScreen.classList.contains('active');
    }
    
    updateProgressDisplay() {
        // 更新章节完成显示
        const chaptersElement = document.getElementById('chaptersCompletedValue');
        if (chaptersElement) {
            chaptersElement.textContent = `${this.stats.chaptersCompleted}/5`;
        }
        
        // 更新分支探索显示
        const branchesElement = document.getElementById('branchesExploredValue');
        if (branchesElement) {
            branchesElement.textContent = `${this.stats.branchesExplored.size}/145+`;
        }
        
        // 更新结局解锁显示
        const endingsElement = document.getElementById('endingsUnlockedValue');
        if (endingsElement) {
            endingsElement.textContent = `${this.stats.endingsUnlocked.size}/20`;
        }
        
        // 更新总游戏时间显示
        const playtimeElement = document.getElementById('totalPlaytimeValue');
        if (playtimeElement) {
            const minutes = Math.floor(this.stats.totalPlaytime / 60000);
            playtimeElement.textContent = `${minutes}分钟`;
        }
    }
    
    // 数据持久化
    saveProgressData() {
        const data = {
            ...this.stats,
            branchesExplored: Array.from(this.stats.branchesExplored),
            endingsUnlocked: Array.from(this.stats.endingsUnlocked),
            achievementsUnlocked: Array.from(this.stats.achievementsUnlocked)
        };
        
        localStorage.setItem('timeEcho_progress', JSON.stringify(data));
    }
    
    loadProgressData() {
        const saved = localStorage.getItem('timeEcho_progress');
        if (saved) {
            try {
                const data = JSON.parse(saved);
                this.stats = {
                    ...this.stats,
                    ...data,
                    branchesExplored: new Set(data.branchesExplored || []),
                    endingsUnlocked: new Set(data.endingsUnlocked || []),
                    achievementsUnlocked: new Set(data.achievementsUnlocked || []),
                    sessionStartTime: Date.now()
                };
            } catch (error) {
                console.warn('进度数据加载失败:', error);
            }
        }
    }
}

// 创建全局实例
window.progressTracker = new ProgressTracker();